package ink.lovejinhu.swardToOffer;

import java.util.HashMap;

/**
 * @author jinhu
 * created 2021-08-09 8:41
 */
public class Offer_035 {


    /**
     * 复制链表的复制
     * 没搞清楚想考啥，一定要new，第一遍没用new
     *
     * @param head
     * @return
     */
    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        HashMap<Node, Node> nodeHashMap = new HashMap<>();
        Node temp = head;
        while (temp != null) {
            nodeHashMap.put(temp, new Node(temp.val));
            temp = temp.next;
        }
        temp = head;
        while (temp != null) {
            nodeHashMap.get(temp).next = nodeHashMap.get(temp.next);
            nodeHashMap.get(temp).random = nodeHashMap.get(temp.random);
            temp = temp.next;
        }
        return nodeHashMap.get(head);

    }

    class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }
}
